package com.hancai.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hancai.constant.Constants;
import com.hancai.mapper.TRecordRemarkMapper;
import com.hancai.model.TRecordRemark;
import com.hancai.query.RecordRemarkQuery;
import com.hancai.service.RecordRemarkService;
import com.hancai.util.JWTUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author super
 */
@Service
public class RecordRemarkServiceImpl implements RecordRemarkService {

    @Resource
    private TRecordRemarkMapper tRecordRemarkMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveRecordRemark(RecordRemarkQuery recordRemarkQuery) {
        TRecordRemark tRecordRemark = new TRecordRemark();

        //把RecordRemarkQuery对象里面的属性数据复制到TRecordRemark对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(recordRemarkQuery, tRecordRemark);
        //创建时间
        tRecordRemark.setCreateTime(new Date());

        //登录人的id
        Integer loginUserId = JWTUtils.parseUserFromJWT(recordRemarkQuery.getToken()).getId();
        //创建人
        tRecordRemark.setCreateBy(loginUserId);

        return tRecordRemarkMapper.insertSelective(tRecordRemark);
    }

    @Override
    public PageInfo<TRecordRemark> getRecordRemarkByPage(Integer current, RecordRemarkQuery recordRemarkQuery) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 2.查询
        List<TRecordRemark> list = tRecordRemarkMapper.selectRecordRemarkByPage(recordRemarkQuery);
        // 3.封装分页数据到PageInfo
        PageInfo<TRecordRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TRecordRemark getRecordRemarkById(Integer id) {
        return tRecordRemarkMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateRecordRemark(RecordRemarkQuery recordRemarkQuery) {
        TRecordRemark tRecordRemark = new TRecordRemark();

        //把RecordRemarkQuery对象里面的属性数据复制到TRecordRemark对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(recordRemarkQuery, tRecordRemark);
        //编辑时间
        tRecordRemark.setEditTime(new Date());

        //登录人的id
        Integer loginUserId = JWTUtils.parseUserFromJWT(recordRemarkQuery.getToken()).getId();
        //创建人
        tRecordRemark.setEditBy(loginUserId);

        return tRecordRemarkMapper.updateByPrimaryKeySelective(tRecordRemark);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delRecordRemarkById(Integer id) {
        //逻辑删除：不删数据，只是修改一下数据的状态，数据依然还在表里面；
        //物理删除：真正的把数据从表里面删掉
        TRecordRemark tRecordRemark = new TRecordRemark();
        tRecordRemark.setId(id);
        //删除状态（null或者0正常，1删除）
        tRecordRemark.setDeleted(1);
        return tRecordRemarkMapper.updateByPrimaryKeySelective(tRecordRemark);
    }
}
